package com.dlwx.parttimetalent.views;

import android.Manifest;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.dlwx.parttimetalent.wechatpay.wxapi.WXShareUtiles;
import com.dlwx.parttimetalent.weibo.WeiboUitls;
import com.dlwx.parttimetalent.wxapi.QQUtiles;
import com.flyco.dialog.widget.base.BottomBaseDialog;
import com.tbruyelle.rxpermissions2.RxPermissions;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.functions.Consumer;

/**
 * Created by Administrator on 2017/11/24.
 */

public class ShareDialog extends BottomBaseDialog {

    @BindView(R.id.tv_wechat_friend)
    TextView tvWechatFriend;
    @BindView(R.id.tv_wechat)
    TextView tvWechat;
    @BindView(R.id.tv_qq)
    TextView tvQq;
    @BindView(R.id.tv_qq_space)
    TextView tvQqSpace;
    @BindView(R.id.tv_weibo)
    TextView tvWeibo;
    @BindView(R.id.tv_link)
    TextView tvLink;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    private Context context;
    private QQUtiles qqUtil;
    private Bitmap bitmap;
    private WXShareUtiles shareUtiles;
    private RxPermissions rxPermissions;
    private String shareUrl="";
    private  ClipboardManager manager;

    public ShareDialog(Context context,String shareUrl) {
        super(context);
        this.context = context;
        this.shareUrl=shareUrl;
         manager= (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    }

    @Override
    public View onCreateView() {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_share, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void setUiBeforShow() {
        qqUtil=new QQUtiles();
        rxPermissions=new RxPermissions((Activity) context);
        bitmap=BitmapFactory.decodeResource(context.getResources(),R.mipmap.logo_share);
        shareUtiles=new WXShareUtiles(context);
    }


    @OnClick({R.id.tv_wechat_friend, R.id.tv_wechat, R.id.tv_qq, R.id.tv_qq_space, R.id.tv_weibo, R.id.tv_link, R.id.tv_cancel})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_wechat_friend:
                shareUtiles.share2weixin(2,bitmap,shareUrl);
                this.dismiss();
                break;
            case R.id.tv_wechat:
                shareUtiles.share2weixin(1,bitmap,shareUrl);
                this.dismiss();
                break;
            case R.id.tv_qq:
                dismiss();
                qqUtil.shareQQ(context,shareUrl);
                break;
            case R.id.tv_qq_space:
                dismiss();
                rxPermissions.request(Manifest.permission.READ_EXTERNAL_STORAGE).subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if(aBoolean){
                            qqUtil.shareImageQQZone(context,shareUrl);
                        }
                    }
                });

                break;
            case R.id.tv_weibo:
                WeiboUitls weiboUitls=new WeiboUitls((Activity) context);
                weiboUitls.share(shareUrl);
                this.dismiss();
                break;
            case R.id.tv_link:
                manager.setPrimaryClip(ClipData.newRawUri(context.getString(R.string.app_name), Uri.parse(shareUrl)));
                ToastUtils.toastForShort(context,"复制成功");
                this.dismiss();
                break;
            case R.id.tv_cancel:
                this.dismiss();
                break;
        }
    }
}
